<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/8/27
 * Time: 14:34
 */

namespace app\admin\controller;

use app\admin\model\Putforward as M;
use think\Cookie;
use think\db;
use think\Exception;

class Putforward extends User
{
    public function index(){
        $m= new M();
        $data= $m->alias('a')
            ->join('member b' ,'b.member_id=a.member_id')
            ->join('member_real c' ,'c.member_id=a.member_id')
            ->join('transferchannel d' ,'d.channel_id=c.channel_id')
            ->field('a.*,b.member_nickname,c.account,d.name')
            ->order('id desc')
            ->paginate(20);
        $this->assign('data',$data);
        return $this->fetch();
    }
    //提现通过
    public function tongguo(){
        $id= input('id');
        //判断是否能够提现
        $putforward = db('putforward')->where(['state'=>1,'id'=>$id])->find();
        if(empty($putforward)) return $this->error('提现信息不存在或已处理，请刷新后重试！');
        $member = db('member')->find($putforward['member_id']);
        if(empty($member)) return $this->error('客户信息错误，请联系管理员！');
        db::startTrans();
        try{
            //修改提现状态
            db('putforward')->where('id',$id)->update([
                'state'=>2,
                'adminid'=>Cookie::get('admin'),
                'uptime'=>time()
            ]);

            //写入流水记录
            $accountData = [
                'member_id'=>$putforward['member_id'],
                'accounts_money'=>$putforward['money'],
                'business_way'=>2,
                'description'=>'客户申请提现，管理员审核通过',
                'creattime'=>date('YmdHis'),
                'admin_id'=>Cookie::get('admin'),
            ];
            db('accounts')->insert($accountData);

            db::commit();
            return $this->success('修改成功');
        }catch(Exception $e){
            db::rollback();
            return $this->error('修改失败');
        }
    }

    //提现申请拒绝
    public function jujue(){
        $id= input('id');
        // 启动事务
        $putforward = db('putforward')->where(['id'=>$id,'state'=>1])->find();
        if(empty($putforward)) return $this->error('提现申请已处理或不存在，请刷新后重试！');
        db::startTrans();
        try{
            db('member')->where('member_id',$putforward['member_id'])->setInc('member_money', (float)$putforward['money']);
            db('putforward')->where('id',$id)->update([
                'state'=>3,
                'uptime'=>time(),
                'adminid'=>Cookie::get('admin')
            ]);
            db::commit();
            return $this->success('修改成功');
        }catch(Exception $e){
            db::rollback();
            return $this->error('修改失败');
        }
    }
}